package com.example.demo1;
import java.sql.*;
public class Dy {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/library_management?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String username = "root"; // 你的数据库用户名
        String password = "123456"; // 你的数据库密码

        String name="admin";
        String pwd="admin123";+
        int uid=0;
        String uemail=null;
        Connection conn=null;
        PreparedStatement prep=null;
        ResultSet rs=null;
        String sql="select * from user where username = ? and password = ? ";

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//加载mysql驱动
            conn = DriverManager.getConnection(url, username, password);

            prep = conn.prepareStatement(sql);//创建数据库工具
            prep.setString(1, name);
            prep.setString(2, pwd);
            //查询结果
            rs = prep.executeQuery();
            while (rs.next()) {
                uid = rs.getInt("id");//=re.getInt(1);
                uemail = rs.getString("email");
            }

            System.out.println(conn);
        } catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        finally {    //先开的后关
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            try {
                prep.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        System.out.println(uid);
        System.out.println(uemail);
    }
}




































































